/**
 * load initial configuration details
 * 
 * reference: http://www.configure-all.com/java_properties.php
 */
package pos.config;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 * @author Parvez
 */
public class LoadConfig {
    
    static String confgFilePath = "config\\config.properties";
    static Properties props = new Properties();
    
    /**
     * default constructor for loading configuration file
     */
    public LoadConfig() {
        load(null);
    }
    
    /**
     * parameterised constructor for loading a custom path
     * 
     * @param customPath String representation of the file path
     */
    public LoadConfig(String customPath) {
        load(customPath);
    }
    
    /**
     * 
     * @param path String representation of the file path
     */
    private void load(String path) {
        
        if(path != null) {  /* check to see if the file path is null. if it is use default path */
            confgFilePath = path;
        }
        //try retrieve data from file
        try {
            props.load(new FileInputStream(confgFilePath));
        }
        //catch exception in case properties file does not exist
        catch(IOException e) {
//            e.printStackTrace();
            System.out.println("Could not find configuration file: " + confgFilePath);
        }
    }
    
    /**
     * 
     * @return Map data structure of database parameters
     */
    public Map<String, String> getDbParams() {
        Map<String, String> params = new HashMap<String, String>();
        
        params.put("user", props.getProperty("admin_user"));
        params.put("pass", props.getProperty("admin_password"));
        params.put("url", props.getProperty("db_url"));
        params.put("host", props.getProperty("db_host"));
        params.put("port", props.getProperty("db_port"));
        params.put("database", props.getProperty("db_database"));
//        System.out.println(message);
        
        return params;
    }
    
    /**
     * 
     * @return Map data structure of external directory paths
     */
    public Map<String, String> getExtDirs() {
        Map<String, String> params = new HashMap<String, String>();
        
        params.put("banners", props.getProperty("banners_dir"));
        params.put("images", props.getProperty("images_dir"));
//        System.out.println(message);
        
        return params;
    }
}
